home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / mvaders / frmmain.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-08-30  |  15.3 KB  |  442 lines

  1. VERSION 2.00
  2. Begin Form frmMain 
  3.    BorderStyle     =   3  'Fixed Double
  4.    Caption         =   "MVaders -- Can You Save The World?  A Game by Mark Meany."
  5.    ClientHeight    =   5460
  6.    ClientLeft      =   990
  7.    ClientTop       =   1770
  8.    ClientWidth     =   7395
  9.    Height          =   6150
  10.    Icon            =   FRMMAIN.FRX:0000
  11.    KeyPreview      =   -1  'True
  12.    Left            =   930
  13.    LinkTopic       =   "Form1"
  14.    MaxButton       =   0   'False
  15.    ScaleHeight     =   5460
  16.    ScaleWidth      =   7395
  17.    Top             =   1140
  18.    Width           =   7515
  19.    Begin PictureBox picLoader 
  20.       AutoRedraw      =   -1  'True
  21.       AutoSize        =   -1  'True
  22.       Height          =   1155
  23.       Left            =   480
  24.       ScaleHeight     =   1125
  25.       ScaleWidth      =   1545
  26.       TabIndex        =   3
  27.       Top             =   5640
  28.       Visible         =   0   'False
  29.       Width           =   1575
  30.    End
  31.    Begin PictureBox picStatus 
  32.       AutoRedraw      =   -1  'True
  33.       BackColor       =   &H00000000&
  34.       Height          =   375
  35.       Left            =   60
  36.       ScaleHeight     =   345
  37.       ScaleWidth      =   7245
  38.       TabIndex        =   1
  39.       Top             =   60
  40.       Width           =   7275
  41.       Begin Label lblDebug 
  42.          BackStyle       =   0  'Transparent
  43.          FontBold        =   -1  'True
  44.          FontItalic      =   0   'False
  45.          FontName        =   "Courier New"
  46.          FontSize        =   8.25
  47.          FontStrikethru  =   0   'False
  48.          FontUnderline   =   0   'False
  49.          ForeColor       =   &H00FFFFFF&
  50.          Height          =   255
  51.          Left            =   420
  52.          TabIndex        =   2
  53.          Top             =   60
  54.          Width           =   6555
  55.       End
  56.    End
  57.    Begin PictureBox picGame 
  58.       AutoRedraw      =   -1  'True
  59.       BackColor       =   &H00000000&
  60.       Height          =   4995
  61.       Left            =   60
  62.       Picture         =   FRMMAIN.FRX:0302
  63.       ScaleHeight     =   331
  64.       ScaleMode       =   3  'Pixel
  65.       ScaleWidth      =   483
  66.       TabIndex        =   0
  67.       Top             =   420
  68.       Width           =   7275
  69.    End
  70.    Begin Timer tmrGameLoop 
  71.       Interval        =   50
  72.       Left            =   3120
  73.       Top             =   2520
  74.    End
  75.    Begin Menu mnuFile 
  76.       Caption         =   "&File"
  77.       Begin Menu mnuFileAbout 
  78.          Caption         =   "&About"
  79.       End
  80.       Begin Menu mnuFileSpacer 
  81.          Caption         =   "-"
  82.       End
  83.       Begin Menu mnuFileExit 
  84.          Caption         =   "E&xit"
  85.       End
  86.    End
  87.    Begin Menu mnuGame 
  88.       Caption         =   "&Game"
  89.       Begin Menu mnuGameNew 
  90.          Caption         =   "&New"
  91.       End
  92.       Begin Menu mnuGamePause 
  93.          Caption         =   "&Pause"
  94.       End
  95.       Begin Menu mnuGameAbort 
  96.          Caption         =   "&Abort"
  97.       End
  98.       Begin Menu mnuGameSpacer 
  99.          Caption         =   "-"
  100.       End
  101.       Begin Menu mnuGameOptions 
  102.          Caption         =   "&Options"
  103.       End
  104.    End
  105. Option Explicit
  106. Dim miBoss As Integer
  107. Sub Form_Activate ()
  108. 'Give initial instructions
  109. ShowGameOver
  110. picGame.Refresh
  111. End Sub
  112. Sub Form_KeyDown (KeyCode As Integer, Shift As Integer)
  113. Debug.Print KeyCode
  114. 'Act on keys we need to monitor
  115. Select Case KeyCode
  116. Case KEY_CUR_LEFT   'Moving left
  117.     giKeyStatus = giKeyStatus Or KEY_CUR_LEFT_FLAG
  118. Case KEY_CUR_RIGHT  'Moving right
  119.     giKeyStatus = giKeyStatus Or KEY_CUR_RIGHT_FLAG
  120. Case KEY_FIRE       'Firing
  121.     'Fire button is disabled
  122.     If giFireLock Then Exit Sub
  123.     'If game is not in progress, start it
  124.     If giGameStatus Then
  125.         If giGameStatus = GAME_STOPPED Then
  126.             picGame.Picture = LoadPicture("")
  127.             giLevel = 1
  128.             giScore = 0
  129.             giLives = 3
  130.             InitL1 0
  131.             tmrGameLoop.Interval = GamePrefs.iTimer
  132.         End If
  133.         giGameStatus = GAME_PLAYING
  134.     'Otherwise flag player is firing
  135.     Else
  136.         giKeyStatus = giKeyStatus Or KEY_FIRE_FLAG
  137.     End If
  138. Case KEY_PAUSE      'Pausing the game
  139.     If Not giGameStatus Then giGameStatus = GAME_PAUSED
  140. Case KEY_ABORT
  141.     giGameStatus = GAME_STOPPED
  142. Case KEY_QUIT       'Quitting the game
  143.     tmrGameLoop.Enabled = False
  144.     frmMain.Hide
  145. End Select
  146. End Sub
  147. Sub Form_KeyUp (KeyCode As Integer, Shift As Integer)
  148. Select Case KeyCode
  149. Case KEY_CUR_LEFT   'Moving left
  150.     giKeyStatus = giKeyStatus And (Not KEY_CUR_LEFT_FLAG)
  151. Case KEY_CUR_RIGHT  'Moving right
  152.     giKeyStatus = giKeyStatus And (Not KEY_CUR_RIGHT_FLAG)
  153. Case KEY_FIRE       'Firing
  154.     giKeyStatus = giKeyStatus And (Not KEY_FIRE_FLAG)
  155.     giFireLock = False
  156. End Select
  157. End Sub
  158. Sub Form_Load ()
  159. 'Center form on the screen
  160. CenterForm Me
  161. End Sub
  162. Sub mnuFileAbout_Click ()
  163. 'If game is in progress then pause it
  164. If giGameStatus = GAME_PLAYING Then giGameStatus = GAME_PAUSED
  165. 'Show the about window
  166. frmAbout.Show VBModal
  167. End Sub
  168. Sub mnuFileExit_Click ()
  169. 'Just hide the form to quit
  170. tmrGameLoop.Enabled = False
  171. frmMain.Hide
  172. End Sub
  173. Sub mnuGameAbort_Click ()
  174. 'Abort no matter what status we are in!
  175. giGameStatus = GAME_STOPPED
  176. End Sub
  177. Sub mnuGameNew_Click ()
  178. 'Abort current game if in progress!
  179. giGameStatus = GAME_STOPPED
  180. DoEvents
  181. 'And start a new one
  182. picGame.Picture = LoadPicture("")
  183. giLevel = 1
  184. giScore = 0
  185. giLives = 3
  186. InitL1 0
  187. tmrGameLoop.Interval = GamePrefs.iTimer
  188. End Sub
  189. Sub mnuGameOptions_Click ()
  190. 'Pause game if in progress
  191. If giGameStatus = GAME_PLAYING Then
  192.     giGameStatus = GAME_PAUSED
  193.     giFireLock = True
  194. End If
  195. 'Allow user to make changes
  196. frmOptions.Show VBModal
  197. End Sub
  198. Sub mnuGamePause_Click ()
  199. 'Pause the game if in progress
  200. If giGameStatus = GAME_PLAYING Then giGameStatus = GAME_PAUSED
  201. End Sub
  202. Sub tmrGameLoop_Timer ()
  203. Dim i As Integer
  204. Dim j As Integer
  205. Dim iDC As Integer
  206. Dim iX As Integer
  207. Dim iY As Integer
  208. Dim iXMin As Integer
  209. Dim iXMax As Integer
  210. Dim iYMax As Integer
  211. Static iDy As Integer
  212. Static iDx As Integer
  213. Static iToggle As Integer
  214. Static iDown As Integer
  215. Static iBonus As Integer
  216. Static iMod As Integer
  217. Dim sDebug As String    'For debug only
  218. 'Initialise invaders speed
  219. If iDx = 0 Then iDx = GamePrefs.iISpeed
  220. If iMod = 0 Then iMod = 10
  221. 'Toggle is used for animation
  222. If iToggle Then iToggle = 0 Else iToggle = 1
  223. 'Build status display
  224. lblDebug = "Lives: " & Format$(giLives, "") & "           HIGH: " & Format$(giHiScore, "00000") & "    Level: " & Format$(giLevel, "00") & "      Score: " & Format$(giScore, "00000")
  225. 'Only process if game running
  226. If giGameStatus = GAME_PLAYING Then
  227.     'Get working DC
  228.     iDC = picGame.hDC
  229.     'Remove sprites
  230.     VBSprRestoreBgrnd iDC
  231.     'Handle bonus ships
  232.     If iBonus = 0 Then
  233.         
  234.         'Reset counter for next bonus ship
  235.         iBonus = Int(Rnd * 40) + 20
  236.         'Display bonus ship
  237.         If gVBSpr(BONUS_SHIP_ID).iActive = False Then VBSprActivateSprite iDC, BONUS_SHIP_ID, 0, 1
  238.     Else
  239.         'Else just dec counter
  240.         iBonus = iBonus - 1
  241.     End If
  242.     'If bonus ship is active, move it
  243.     If gVBSpr(BONUS_SHIP_ID).iActive Then
  244.         VBSprMoveSpriteRel BONUS_SHIP_ID, 8, 0, 18 + iToggle
  245.         If gVBSpr(BONUS_SHIP_ID).iX > ((picGame.Width \ Screen.TwipsPerPixelX) - 24) Then VBSprDeactivateSprite BONUS_SHIP_ID
  246.     End If
  247.     'If explosion is active, animate it until all 3 frames shown
  248.     If gVBSpr(EXPLOSION_ID).iActive Then
  249.         gVBSpr(EXPLOSION_ID).iUser1 = gVBSpr(EXPLOSION_ID).iUser1 + 1
  250.         If gVBSpr(EXPLOSION_ID).iUser1 = 3 Then
  251.             gVBSpr(EXPLOSION_ID).iActive = False
  252.         Else
  253.             VBSprAnimateSprite EXPLOSION_ID, 14 + gVBSpr(EXPLOSION_ID).iUser1
  254.         End If
  255.     End If
  256.     'Animate invaders
  257.     For i = FIRST_INVADER_ID To LAST_INVADER_ID
  258.         If gVBSpr(i).iW > 50 Then
  259.             VBSprAnimateSprite i, 20 + iToggle
  260.         Else
  261.             VBSprAnimateSprite i, 2 * ((i - FIRST_INVADER_ID) \ 6) + iToggle
  262.         End If
  263.     Next i
  264.     'Move the invaders
  265.     If iDy Then
  266.         'Move invaders down, they are at the edge of the screen!
  267.         iYMax = -1
  268.         For i = FIRST_INVADER_ID To LAST_INVADER_ID
  269.             If gVBSpr(i).iActive Then
  270.                 gVBSpr(i).iY = gVBSpr(i).iY + iDy
  271.                 If (gVBSpr(i).iY + gVBSpr(i).iH) > iYMax Then iYMax = gVBSpr(i).iY + gVBSpr(i).iH
  272.             End If
  273.         Next i
  274.         iDy = 0
  275.     Else
  276.         'Normal moving
  277.         iXMin = 9999
  278.         iXMax = -1
  279.         For i = FIRST_INVADER_ID To LAST_INVADER_ID
  280.             If gVBSpr(i).iActive Then
  281.                 gVBSpr(i).iX = gVBSpr(i).iX + iDx
  282.                 If (gVBSpr(i).iX + gVBSpr(i).iW) > iXMax Then iXMax = gVBSpr(i).iX + gVBSpr(i).iW
  283.                 If gVBSpr(i).iX < iXMin Then iXMin = gVBSpr(i).iX
  284.             
  285.                 'Random invader shooting
  286.                 If Rnd > GamePrefs.fIBFreq Then
  287.                     For j = FIRST_INVADER_BULLET_ID To LAST_INVADER_BULLET_ID
  288.                         If gVBSpr(j).iActive = False Then
  289.                             VBSprActivateSprite iDC, j, gVBSpr(i).iX, gVBSpr(i).iY
  290.                             Exit For
  291.                         End If
  292.                     Next j
  293.                 End If
  294.             End If
  295.         Next i
  296.         'Test if we need to move down on next frame, sets iDy if so
  297.         If (iXMin <= 0) Or (iXMax >= (picGame.Width \ Screen.TwipsPerPixelX - 10)) Then
  298.             iDx = iDx * -1
  299.             iDy = GamePrefs.iIDrop
  300.         End If
  301.     End If
  302.     'End game if vaders have landed
  303.     If iYMax >= gVBSpr(PLAYER_ID).iY Then
  304.         i = sndPlaySound(ByVal CStr(APP.Path & "\landed.wav"), SND_ASYNC)
  305.         giGameStatus = GAME_STOPPED
  306.         If giScore > giHiScore Then giHiScore = giScore
  307.         iDx = GamePrefs.iISpeed
  308.         iDy = 0
  309.         iDown = 0
  310.         iMod = 10
  311.         giFireLock = True
  312.     End If
  313.     'Moves players ship
  314.     If giKeyStatus And KEY_CUR_LEFT_FLAG Then iX = -1 * GamePrefs.iPSpeed
  315.     If giKeyStatus And KEY_CUR_RIGHT_FLAG Then iX = GamePrefs.iPSpeed
  316.     VBSprMoveSpriteRel PLAYER_ID, iX, 0, 10 + iToggle
  317.     'If there is a bullet process it
  318.     If giFiring Then
  319.         'If bullet has reached top of display deactivate it
  320.         If gVBSpr(BULLET_ID).iY <= 0 Then
  321.             VBSprDeactivateSprite BULLET_ID
  322.             giFiring = False
  323.         End If
  324.         'Move the bullet
  325.         VBSprMoveSpriteRel BULLET_ID, 0, -1 * GamePrefs.iPBSpeed, 12 + iToggle
  326.         
  327.         'Check for bullet/invaders collision
  328.         For i = FIRST_INVADER_ID To LAST_INVADER_ID
  329.             If gVBSpr(i).iActive Then
  330.                 'Is there a hit
  331.                 If iVBSprCollision(BULLET_ID, i) Then
  332.                     
  333.                     'Only kill invader if hit enough times
  334.                     gVBSpr(i).iUser1 = gVBSpr(i).iUser1 - 1
  335.                     If gVBSpr(i).iUser1 = 0 Then
  336.                         VBSprDeactivateSprite i
  337.                         giScore = giScore + 5
  338.                         giInvaders = giInvaders - 1
  339.                         
  340.                         'Start explosion
  341.                         VBSprAnimateSprite EXPLOSION_ID, 14
  342.                         VBSprActivateSprite iDC, EXPLOSION_ID, gVBSpr(i).iX + gVBSpr(i).iW \ 2, gVBSpr(i).iY + gVBSpr(i).iH \ 2
  343.                         gVBSpr(EXPLOSION_ID).iUser1 = 0
  344.                         'Speed up invaders as they get killed
  345.                         If (giInvaders Mod iMod) = 0 Then iDx = iDx + iDx \ 2
  346.                         If giInvaders = 1 Then iDx = iDx + iDx \ 2
  347.                     End If
  348.                     'Deactivate the bullet sprite
  349.                     VBSprDeactivateSprite BULLET_ID
  350.                     
  351.                     giFiring = False
  352.             
  353.                     'Sound fx
  354.                     PlayHitMe
  355.                     'Prepare next level if all invaders killed
  356.                     If giInvaders = 0 Then
  357.                         If iDown < 100 Then iDown = iDown + 10
  358.                         iDx = GamePrefs.iISpeed
  359.                         giLevel = giLevel + 1
  360.                         If giLevel Mod 5 = 0 Then
  361.                             iMod = 3
  362.                             InitL2 giLevel \ 5
  363.                         Else
  364.                             iMod = 10
  365.                             InitL1 iDown
  366.                         End If
  367.                         giFireLock = True
  368.                     End If
  369.                     
  370.                     'Dont need to check rest of invaders!
  371.                     Exit For
  372.                 End If
  373.             End If
  374.         Next i
  375.         
  376.         'Check for bonus ship/bullet collision
  377.         If gVBSpr(BONUS_SHIP_ID).iActive Then
  378.             If iVBSprCollision(BULLET_ID, BONUS_SHIP_ID) Then
  379.                 'Deactivate the bullet sprite
  380.                 VBSprDeactivateSprite BULLET_ID
  381.                 VBSprDeactivateSprite BONUS_SHIP_ID
  382.                 giFiring = False
  383.             
  384.                 'Start explosion
  385.                 VBSprAnimateSprite EXPLOSION_ID, 14
  386.                 VBSprActivateSprite iDC, EXPLOSION_ID, gVBSpr(BONUS_SHIP_ID).iX, gVBSpr(BONUS_SHIP_ID).iY
  387.                 gVBSpr(EXPLOSION_ID).iUser1 = 0
  388.                 'Sound Fx and scoring, note score increases with level
  389.                 PlayHitMe
  390.                 giScore = giScore + 10 * giLevel
  391.             End If
  392.         End If
  393.     'See if we user wants to fire
  394.     Else
  395.         If giKeyStatus And KEY_FIRE_FLAG Then
  396.         
  397.             'Activate the players bullet
  398.             VBSprActivateSprite iDC, BULLET_ID, gVBSpr(0).iX + 12, gVBSpr(0).iY - 18
  399.             giFiring = True
  400.         End If
  401.     End If
  402.     'Move the invaders bullets
  403.     For j = FIRST_INVADER_BULLET_ID To LAST_INVADER_BULLET_ID
  404.         If gVBSpr(j).iActive Then
  405.             gVBSpr(j).iY = gVBSpr(j).iY + GamePrefs.iIBSpeed
  406.             'Check for off bottom
  407.             If gVBSpr(j).iY >= (picGame.Height \ Screen.TwipsPerPixelY) Then VBSprDeactivateSprite j
  408.             'Check for hit player
  409.             If iVBSprCollision(PLAYER_ID, j) Then
  410.                 i = sndPlaySound(ByVal CStr(APP.Path & "\hitship.wav"), SND_ASYNC)
  411.                 giLives = giLives - 1
  412.                 'All lives gone then game finishes
  413.                 If giLives = 0 Then
  414.                     If giScore > giHiScore Then giHiScore = giScore
  415.                     iDx = GamePrefs.iISpeed
  416.                     iDy = 0
  417.                     iDown = 0
  418.                     giGameStatus = GAME_STOPPED
  419.                     giFireLock = True
  420.                 Else
  421.                     'Clear all alien bullet to give player a chance
  422.                     VBSprDeactivateSprite FIRST_INVADER_BULLET_ID
  423.                     VBSprDeactivateSprite FIRST_INVADER_BULLET_ID + 1
  424.                     VBSprDeactivateSprite FIRST_INVADER_BULLET_ID + 2
  425.                     giGameStatus = GAME_PAUSED
  426.                     giFireLock = True
  427.                 End If
  428.                 Exit For
  429.             End If
  430.         End If
  431.     Next j
  432.     'Redraw sprites
  433.     VBSprDrawSprites iDC
  434.     'Update the display
  435.     picGame.Refresh
  436. ElseIf giGameStatus = GAME_STOPPED Then
  437.     'Game is stopped so tell user how to start it!!!
  438.     ShowGameOver
  439.     picGame.Refresh
  440. End If
  441. End Sub
  442.